package com.dianrui.fmi.activity;

import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.BottomSheetDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.util.MyUtil;
import com.dianrui.fmi.util.ToastUtil;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.share.Sharer;
import com.facebook.share.model.ShareLinkContent;
import com.facebook.share.widget.ShareDialog;
import com.twitter.sdk.android.tweetcomposer.TweetComposer;

import java.net.MalformedURLException;
import java.net.URL;

import butterknife.BindView;
import butterknife.OnClick;

/**
 *
 */

public class StockInfoActivity extends BaseActivity {
    private ShareDialog shareDialog;
    private CallbackManager callbackManager;

    @BindView(R.id.title)
    TextView title;

    @Override
    public int getContentViewId() {
        return R.layout.activity_stock_info;
    }

    @Override
    public void initView() {
        title.setText("Stock Name");
        initFacebook();
    }



    @OnClick({R.id.back, R.id.right_img1, R.id.right_img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.right_img1:
                final BottomSheetDialog bottomSheetDialog=new BottomSheetDialog(StockInfoActivity.this);
                View showThanksMoneyview = LayoutInflater.from(StockInfoActivity.this).inflate(R.layout.dialog_invite_friends, null);
                Button cancelBtn=showThanksMoneyview.findViewById(R.id.cancel);
                LinearLayout facebookLayout=showThanksMoneyview.findViewById(R.id.facebook_layout);
                LinearLayout twitterLayout=showThanksMoneyview.findViewById(R.id.twitter_layout);
                facebookLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomSheetDialog.dismiss();
                        if (!MyUtil.checkfackboosInstalled(StockInfoActivity.this)) {
                            ToastUtil.showToast(getString(R.string.please_install_facebook));
                            return;
                        }
                        if (ShareDialog.canShow(ShareLinkContent.class)) {
                            ShareLinkContent linkContent = new ShareLinkContent.Builder()
                                    .setContentUrl(Uri.parse("https://developers.facebook.com"))
                                    .build();
                            shareDialog.show(linkContent);
                        }
                    }
                });
                twitterLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomSheetDialog.dismiss();
                        if (!MyUtil.checkfackboosInstalled(StockInfoActivity.this)) {
                            ToastUtil.showToast(getString(R.string.please_install_twitter));
                            return;
                        }
                        try {
                            TweetComposer.Builder builder = new TweetComposer.Builder(StockInfoActivity.this)
                                    .url(new URL("https://www.google.com/"));
                            builder.show();
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        }
                    }
                });
                cancelBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomSheetDialog.dismiss();
                    }
                });
                bottomSheetDialog.setContentView(showThanksMoneyview);
                bottomSheetDialog.setCanceledOnTouchOutside(false);
                if (!MyUtil.getActivity(bottomSheetDialog.getContext()).isFinishing()) {
                    bottomSheetDialog.show();
                }
                break;
            case R.id.right_img:
                JumpActivitys(SearchActivity.class);
                break;
        }
    }

    /**
     * facebook配置
     */
    private void initFacebook() {
        callbackManager = CallbackManager.Factory.create();
        shareDialog = new ShareDialog(this);
        // this part is optional
        shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {

            @Override
            public void onSuccess(Sharer.Result result) {
                //分享成功的回调，在这里做一些自己的逻辑处理
                ToastUtil.showToast("Share Success");
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {

            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //facebook的界面回调
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
}
